package com.fadadianqi.logistics.admin.controller;

import com.yeqiangwei.commons.util.DateTimeUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateTimeHelper {

    public static void main(String args[]) {
        System.out.println(DateTimeHelper.getOfMonthStart(2));
        System.out.println(DateTimeHelper.getOfMonthEnd(2));
        //System.out.println(DateTimeUtils.getCurrentMonth());
    }

    public static final Date toDate(String s, String fmt) {
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        try {
            return sdf.parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static final Date getOfMonthStart(int year, int month) {
        long time = DateTimeUtils.getTimeInMillisForDayBegin(year, month, 1);
        return new Date(time);
    }

    public static final Date getOfMonthStart(int month) {
        int year = DateTimeUtils.getCurrentYear();
        long time = DateTimeUtils.getTimeInMillisForDayBegin(year, month, 1);
        return new Date(time);
    }

    public static final Date getOfMonthEnd(int year, int month) {
        int day = DateTimeUtils.getLastDayOfMonth(year, month);
        long time = DateTimeUtils.getTimeInMillisForDayEnd(year, month, day);
        return new Date(time);
    }

    public static final Date getOfMonthEnd(int month) {
        int year = DateTimeUtils.getCurrentYear();
        int day = DateTimeUtils.getLastDayOfMonth(year, month);
        long time = DateTimeUtils.getTimeInMillisForDayEnd(year, month, day);
        return new Date(time);
    }


    public static Date getOfDayStart(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date.getTime());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        return new Date(calendar.getTimeInMillis());
    }

    public static Date getOfDayEnd(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date.getTime());
        calendar.set(Calendar.HOUR_OF_DAY, 23);
        calendar.set(Calendar.MINUTE, 59);
        calendar.set(Calendar.SECOND, 59);
        return new Date(calendar.getTimeInMillis());
    }
}
